package cn.innosmart.aq.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import java.util.ArrayList;

import cn.innosmart.aq.R;
import cn.innosmart.aq.bean.BoxBean;

/**
 * Created by David on 2016/1/12.
 */
public class AssignBoxAdapter extends BaseAdapter {

    public boolean isModify;
    private ArrayList<BoxBean> boxBeans;
    private Context context;
    private LayoutInflater mInflater;
    private CompoundButton.OnCheckedChangeListener onCheckedChangeListener;
    private ArrayList<String> checkBoxes;

    public AssignBoxAdapter(Context context, ArrayList<BoxBean> boxBeans) {
        this.context = context;
        this.mInflater = LayoutInflater.from(context);
        this.boxBeans = boxBeans;
        this.checkBoxes = new ArrayList<>();
    }

    /**
     * 设置指派界面勾选框事件
     *
     * @param onCheckedChangeListener
     */
    public void setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener onCheckedChangeListener) {
        this.onCheckedChangeListener = onCheckedChangeListener;
    }

    @Override
    public int getCount() {
        return boxBeans.size();
    }

    @Override
    public Object getItem(int position) {
        return boxBeans.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.listview_assign_box, null);
//            holder.rlAssign = (RelativeLayout) convertView.findViewById(R.id.rl_assign);
            holder.cbAssign = (CheckBox) convertView.findViewById(R.id.cb_assign);
            holder.tvBoxTitle = (TextView) convertView.findViewById(R.id.tv_box_title);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        BoxBean boxBean = boxBeans.get(position);
        holder.tvBoxTitle.setText(boxBean.getTitle());
        holder.cbAssign.setTag(boxBean);
        if (checkBoxes.contains(boxBean.getHubId())) {
            holder.cbAssign.setChecked(true);
        } else {
            holder.cbAssign.setChecked(false);
        }
        if (onCheckedChangeListener != null)
            holder.cbAssign.setOnCheckedChangeListener(onCheckedChangeListener);
        return convertView;
    }

    /**
     * 进入修改模式
     *
     * @param startModify
     */
    public void startModify(boolean startModify) {
        this.isModify = startModify;
        notifyDataSetChanged();
    }

    /**
     * 更新UI界面
     *
     * @param checkBoxes
     */
    public void refresh(ArrayList<String> checkBoxes) {
        this.checkBoxes = checkBoxes;
        notifyDataSetChanged();
    }

    static class ViewHolder {
        TextView tvBoxTitle;
        CheckBox cbAssign;
    }
}
